package main

import (
	"fmt"
	"sync"
	"time"
)

var num int
var wg1 sync.WaitGroup
var rw sync.RWMutex

func read() {

	for {
		rw.RLock()
		for i := 0; i < 10; i++ {
			fmt.Println("read num", num)
			time.Sleep(time.Millisecond * 50)
		}
		rw.RUnlock()
	}
	defer wg1.Done()
}

func write() {

	for {
		rw.Lock()
		time.Sleep(time.Millisecond * 500)
		fmt.Println("write num", num)
		time.Sleep(time.Second * 5)
		rw.Unlock()
	}
	defer wg1.Done()
}

func main() {
	wg1.Add(2)
	go read()
	go write()
	wg1.Wait()
}
